<template>
  <div @mouseleave="leaveShow" @mouseenter="enterShow">
    <h2 class="all">全部商品分类</h2>
    <!-- 过渡动画 -->

    <div class="sort" v-show="show">
      <div class="all-sort-list2" @click="goSearch">
        <div
          class="item"
          v-for="(c1, index) in categoryList"
          :key="c1.categoryId"
        >
          <h3
            @mouseenter="enterIndex(index)"
            :class="{ 'item-hover': index == currentIndex }"
          >
            <a :data-Name="c1.categoryName" :data-level1="c1.categoryId">{{
              c1.categoryName
            }}</a>
          </h3>

          <div
            class="item-list clearfix"
            :style="{ display: currentIndex == index ? 'block' : 'none' }"
          >
            <div
              class="subitem"
              v-for="c2 in c1.categoryChild"
              :key="c2.categoryId"
            >
              <dl class="fore">
                <dt>
                  <a
                    :data-Name="c2.categoryName"
                    :data-level2="c2.categoryId"
                    >{{ c2.categoryName }}</a
                  >
                </dt>
                <dd>
                  <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                    <a
                      :data-Name="c3.categoryName"
                      :data-level3="c3.categoryId"
                      >{{ c3.categoryName }}</a
                    >
                  </em>
                </dd>
              </dl>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, onMounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";

const $store = useStore();
const $state = $store.state;
const $route = useRoute();
const $router = useRouter();

let show = ref(true);
let currentIndex = ref(-1);

let categoryList = computed(() => {
  return $state.Home.categoryList;
});

//主页-鼠标进入防抖函数
const enterIndex = (index) => {
  currentIndex.value = index; //选中的列表项
};
//主页-鼠标离开
const leaveShow = () => {
  currentIndex.value = -1; //主页-鼠标离开
  if ($route.path != "/home") {
    show.value = false; //搜索页-鼠标离开隐藏列表
  }
};
//搜索页-列表路由跳转性能优化：事件委派 + 自定义标签属性
const goSearch = (e) => {
  let { name, level1, level2, level3 } = e.target.dataset; //获取标签
  if (name) {
    //判断标签是否自定义name
    let location = { name: "Search" };
    let query = { categoryName: name };
    if (level1) {
      query.categoryId = level1;
    } else if (level2) {
      query.categoryId = level2;
    } else {
      query.categoryId = level3;
    }
    location.query = query;
    let params = $route.params; //获取遗留下来的params参数
    if (params) {
      location.params = params;
    }

    $router.push(location); //路由跳转道搜索页
  }
};

//搜索页-鼠标进入显示列表
const enterShow = () => {
  if ($route.path != "/home") {
    show.value = true; //搜索页-显示列表
  }
};
</script>

<style scoped lang="less">
.all {
  width: 210px;
  height: 45px;
  background-color: #3498db;
  line-height: 45px;
  text-align: center;
  color: #fff;
  font-size: 14px;
  font-weight: bold;
}
.sort {
  position: absolute;
  left: 0;
  top: 45px;
  width: 210px;
  height: 461px;
  position: absolute;
  background: #fafafa;
  z-index: 999;
  .all-sort-list2 {
    height: 100%;
    overflow: auto;
    overflow-y: scroll;
    .item {
      h3 {
        line-height: 30px;
        font-size: 14px;
        font-weight: 400;
        overflow: hidden;
        padding: 0 20px;
        margin: 0;

        a {
          color: #333;
        }
      }

      .item-list {
        display: none;
        position: absolute;
        width: 734px;
        min-height: 460px;
        background: #f7f7f7;
        left: 210px;
        border: 1px solid #ddd;
        top: 0;
        z-index: 9999 !important;

        .subitem {
          float: left;
          width: 650px;
          padding: 0 4px 0 8px;

          dl {
            border-top: 1px solid #eee;
            padding: 6px 0;
            overflow: hidden;
            zoom: 1;

            &.fore {
              border-top: 0;
            }

            dt {
              float: left;
              width: 54px;
              line-height: 22px;
              text-align: right;
              padding: 3px 6px 0 0;
              font-weight: 700;
            }

            dd {
              float: left;
              width: 415px;
              padding: 3px 0 0;
              overflow: hidden;

              em {
                float: left;
                height: 14px;
                line-height: 14px;
                padding: 0 8px;
                margin-top: 5px;
                border-left: 1px solid #ccc;
              }
            }
          }
        }
      }
    }
    .item-hover {
      background-color: #73c3f8;
    }
  }
}

.sort-enter-active {
  animation: height-in 0.5s;
}
.sort-leave-active {
  animation: height-in 0.2s reverse;
}
@keyframes height-in {
  0% {
    height: 0px;
  }

  100% {
    height: 461px;
  }
}
</style>
